////////////////////////////////////////////////
// DESCRIPTION:
//     Support for implementing Installable ISRs
//
// Legal Notices:
//     Copyright (c) 2008, Telliam Consulting, LLC.
//     All rights reserved.
//
//     Redistribution and use in source and binary forms, with or without modification,
//     are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//
//     * Redistributions in binary form must reproduce the above copyright notice,
//       this list of conditions and the following disclaimer in the documentation
//       and/or other materials provided with the distribution.
//
//     * Neither the name of Telliam Consulting nor the names of its contributors
//       may be used to endorse or promote products derived from this software
//       without specific prior written permission.
//
// Disclaimer:
//     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//     IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
//     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
//     OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
//     OF THE POSSIBILITY OF SUCH DAMAGE.  
//

#include <FusionWare\DFX\IISRAPI.h>  // MUST ALWAYS be FIRST INCLUDE!

// don't use macros for this as it is implemented locally here
#undef NKDbgPrintfW


// heap for the IISR 16K should be enough for most IISRs
static BYTE IISRHeap[16384];

namespace FusionWare
{
    namespace DFX
    {
        namespace IISRAPI
        {
            CeLogImportTableV2 g_Imports;
        }
    }
}

using namespace FusionWare::DFX::IISRAPI;
 
/////////////////////////////////////////////////////////////////////////
// Description:
//    Entry to KernelLibIoControl from kernel mode code (trust already verified). 
//
// Remarks:
//    User mode code will call into the SC_KernelLibIoControl via trap so that 
//    trust can be verified.
//
//    A pointer to NKKernelLibIoControl is provided as the reserved param
//    to DllEntry on ISR Dlls. 
//
//    The preset "handle" KMOD_OAL can be use to send messages to OEMIoControl in the OAL
//
typedef BOOL (*NKKernelLibIoControlPtr)(HANDLE hLib, DWORD IoControlCode, void* pInBuf, DWORD  InBufSize, void* pOutBuf, DWORD  OutBufSize,DWORD* pBytesReturned);

static NKKernelLibIoControlPtr NKKernelLibIoControl=NULL;

extern "C" {

    ///////////////////////////////////////////////////////
    // {internal}
    // Description: 
    // 
    // Input: 
    //    hinstDll -  Instance handle 
    //    Reason   -  reason for the call
    //    pNKFunc  -  Pointer to NKKernelLibIoControl
    //
    // Remarks: 
    //    When a DLL is loaded into the kernel via LoadIntChainHandler 
    //    the reserved pointer becomes a pointer to NKKernelIoControl
    //    in the kernel. This can be used to access Kernel specific
    //    functionality and the OAL via KMOD_OAL to send control codes 
    //    to OEMIoControl
    //
    // Note: 
    //    This is OS version dependent and needs testing on each OS version
    //
    BOOL __stdcall DllEntry(HINSTANCE hinstDll, DWORD Reason, NKKernelLibIoControlPtr pNKFunc)
    {
        if(DLL_PROCESS_ATTACH == Reason )
        {
            if(pNKFunc==NULL)
            {
                NKKernelLibIoControl = NULL;
                return FALSE;
            }
            
            NKKernelLibIoControl = pNKFunc;
        }

        // Get imports from kernel 
        g_Imports.dwVersion = CELOG_IMPORT_VERSION;
        if(!NKKernelLibIoControl((HANDLE)KMOD_CELOG, IOCTL_CELOG_IMPORT, &g_Imports, sizeof(CeLogImportTableV2), NULL, 0, NULL))
        {
            // Can't DEBUGMSG or anything here because the import table is needed to do that
            return FALSE;
        }
        
        FusionWare::CRT::StaticInit((DWORD)&IISRHeap, sizeof(IISRHeap));

        return TRUE;
    }

	// Need to declare this here so the FusionWareCRT Heap code can link to it since COREDLL isn't present
    // and FusionWareCRT is a lib that was not built including IISRAPI.h to get the wrapper versions of the
    // functions
    //
    void NKDbgPrintfW(LPCWSTR lpszFmt,...)  
    {
        va_list arglist;
        va_start(arglist, lpszFmt);
        NKvDbgPrintfW(lpszFmt, arglist);
        va_end(arglist);
    }
}